﻿Public Class Editor
    Private Sub EnsureMinimumSize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ResizeEnd
        If Me.Width <= 300 Then
            Me.Width = 300
            PAD.Width = Me.Width - 17
        End If
        If Me.Height <= 300 Then
            Me.Height = 300
            PAD.Height = Me.Height - 35
        End If
    End Sub
    Private Sub Resizepad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        PAD.Height = Me.Height - 35
        PAD.Width = Me.Width - 17
    End Sub

    Private Sub SilentClose(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If Debugger.Closing = False Then
            e.Cancel = True
            Debugger.EditorOpen = False
            Me.Hide()
        End If
    End Sub

    Private Sub EnsurePadSize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PAD.Width = Me.Width - 17
        PAD.Height = Me.Height - 35
    End Sub

    Private Sub VerifyChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PAD.TextChanged
        Me.Text = "Debugger - Changed"
        Debugger.FileChanged = True
        SyntaxHighlight_Line()
    End Sub
    Private Sub SyntaxHighlight_Line()
        Dim LastCursor As Integer = PAD.SelectionStart
        Dim StartRow As Integer = PAD.SelectionStart
        Dim CurrentRow As Integer = PAD.SelectionStart + 1
        Dim bob As String
        Dim bob2 As Integer
        Dim Found As Boolean = False
        While CurrentRow > 1
            CurrentRow -= 1
            bob = Mid(PAD.Text, CurrentRow, 1)
            bob2 = Asc(bob)
            If Mid(PAD.Text, CurrentRow, 1) = Chr(13) Or Mid(PAD.Text, CurrentRow, 1) = Chr(10) Then
                Found = True
                StartRow = CurrentRow
                Exit While
            End If
        End While
        If Found = True Then
            Found = False
            While CurrentRow < Len(PAD.Text) - 1
                CurrentRow += 1
                If Mid(PAD.Text, CurrentRow, 1) = Chr(13) Or Mid(PAD.Text, CurrentRow, 1) = Chr(10) Then
                    Found = True
                    Exit While
                End If
            End While
            If Found = True Then
                SyntaxHighlight(StartRow, CurrentRow, LastCursor)
            End If
        End If
    End Sub
    Private Sub SyntaxHighlight(ByVal StartPos As Integer, ByVal EndPos As Integer, ByVal Cursor As Integer)
        Dim CommentFlag, BracketFlag, QuoteFlag, ValueFlag As SByte
        Dim CurChar As String
        For StartPos = StartPos To EndPos Step 1
            CurChar = Mid(PAD.Text, StartPos, 1)
            If Mid(PAD.Text, StartPos, 4) = "<!--" Then
                CommentFlag = 1
            ElseIf Mid(PAD.Text, StartPos, 3) = "-->" Then
                CommentFlag = -3
            ElseIf CurChar = "<" Then
                BracketFlag = 2
            ElseIf CurChar = ">" Then
                ValueFlag = 0
                BracketFlag = 0
            ElseIf CurChar = Chr(34) Then 'quotes
                If QuoteFlag = 2 Then
                    QuoteFlag = 0
                ElseIf QuoteFlag = 1 Then
                    QuoteFlag = 3
                Else
                    QuoteFlag = 2
                End If
            End If
            PAD.SelectionStart = StartPos - 1
            PAD.SelectionLength = 1
            If CommentFlag = 1 Or CommentFlag < 0 Then
                PAD.SelectionColor = Color.LightPink
                If CommentFlag < 0 Then CommentFlag += 1
            ElseIf QuoteFlag > 1 Or QuoteFlag = 0 Then
                PAD.SelectionColor = Color.LightGreen
                If QuoteFlag = 0 Then QuoteFlag = -1
                If QuoteFlag = 2 Then QuoteFlag = 1
                If QuoteFlag = 3 Then QuoteFlag = -1
            ElseIf QuoteFlag = 1 Then
                PAD.SelectionColor = Color.Green
            ElseIf BracketFlag = 2 Or BracketFlag = 0 Then
                PAD.SelectionColor = Color.Cyan
                If BracketFlag = 0 Then BracketFlag = -1
                If BracketFlag = 2 Then BracketFlag = 1
            ElseIf BracketFlag = 1 Then
                PAD.SelectionColor = Color.DarkCyan
            ElseIf ValueFlag = 1 Then
                PAD.SelectionColor = Color.Red
            Else
                PAD.SelectionColor = Color.Tan
            End If
        Next
        PAD.SelectionStart = Cursor : PAD.SelectionLength = 0
    End Sub
End Class